//
//  ByNewsVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/11.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByNewsVC.h"
#import "ByAccountDetailVC.h"
#import "JSONKit.h"

@interface ByNewsVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ByNewsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"消息";
    
    [self initViews];
    //下拉刷新数据
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self loadData];
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = BY_USER.uid;
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    [ByApiUser messageQuery:netModel success:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert hideHud];
        self.dataArray = result.data;
        [self.tableView reloadData];
        
        
    } failure:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)initViews{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 10) style:UITableViewStylePlain];
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    
    UIButton *detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.contentView addSubview:detailBtn];
    [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cell.contentView).offset(-7);
        make.top.equalTo(cell.contentView).offset(10);
        make.size.mas_equalTo(CGSizeMake(15 * 4, 20));
    }];
    detailBtn.titleLabel.font = FONT(14);
    [detailBtn setTitle:@"点击查看" forState:UIControlStateNormal];
    [detailBtn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    detailBtn.tag = 200 + indexPath.row;
    [detailBtn addTarget:self action:@selector(detailAciton:) forControlEvents:UIControlEventTouchUpInside];
    detailBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    UILabel *stateLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:stateLabel];
    [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cell.contentView).offset(-10);
        make.bottom.equalTo(cell.contentView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(40, 20));
    }];
    stateLabel.font = FONT(14);
    stateLabel.textColor = TEXT_COLOR;
    stateLabel.text = @"未读";
    stateLabel.textAlignment = NSTextAlignmentRight;
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.top.equalTo(detailBtn.mas_top).offset(0);
        make.right.equalTo(detailBtn.mas_left).offset(-10);
        make.height.mas_equalTo(@20);
    }];
    titleLabel.text = @"你有一个存疑查看";
    titleLabel.font = FONT(14);
    titleLabel.textColor = TEXT_COLOR;
    
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left).offset(0);
        make.bottom.equalTo(stateLabel.mas_bottom).offset(0);
        make.right.equalTo(stateLabel.mas_left).offset(-10);
        make.height.mas_equalTo(@20);
    }];
    
    timeLabel.font = FONT(14);
    timeLabel.textColor = [UIColor lightGrayColor];
    
    UILabel *line = [[UILabel alloc] init];
    [cell.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(0);
        make.right.equalTo(cell.contentView).offset(0);
        make.bottom.equalTo(cell.contentView).offset(0);
        make.height.mas_equalTo(@1);
    }];
    line.backgroundColor = BY_LINE_COLOR;
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:netModel.create_time.integerValue]];
    titleLabel.text = netModel.content;
    if (netModel.msg_status.integerValue == 0) {
        stateLabel.text = @"未读";
        stateLabel.textColor = [UIColor redColor];
    }else if (netModel.msg_status.integerValue == 1){
         stateLabel.text = @"已读";
         stateLabel.textColor = GREEN_COLOR;
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

#pragma -mark- UITableViewDelegate
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
        netModel.uid = BY_USER.uid;
        [ByCoreToolAlert showLoadind:@"删除中" hasMasker:YES];
        [ByApiNews messageDel:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showSuccessWithText:@"删除成功"];
            [self loadData];
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
}

- (void)detailAciton:(UIButton *)btn{
    NSLog(@"点击查看");
    ByNetModel *netModel = [self.dataArray objectAtIndex:btn.tag - 200];
    netModel.uid = BY_USER.uid;
    if (netModel.extra && netModel.extra.length > 0 && ![netModel.extra isEqualToString:@"[]"]) {
       NSMutableDictionary *dic = [netModel.extra mutableObjectFromJSONString];
       netModel.item_id = dic[@"item_id"];
    }
    [ByApiNews JgbRecordInfo:netModel success:^(ByNetBaseResult *result){
        
        ByNetModel *model = result.data;
        ByAccountDetailVC *vc = [[ByAccountDetailVC alloc] init];
        vc.netModel = model;
        vc.type = 1;
        [self by_normalPush:vc hasNavi:YES];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
    [ByApiNews messageInfo:netModel success:^(ByNetBaseResult *result) {
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
